/*
 * @ author madman 
 * @ use 导入默认数据
 */
const AdminModel = require('./models/system/admin.model');
const PermissionsModel = require('./models/system/permissions.model');
const RolesModel = require('./models/system/roles.model');
const SystemModel = require('./models/system/system.model');
const type_id = require("./models/type");
const adminJson = require("../data/admin");
const powerJson = require("../data/power");
const rolesJson = require("../data/roles");
const specJson = require("../data/spec");
const sysJson = require("../data/sys");
class dataImport {
    // 导入数据
    async import (ctx, next) {
        const _typeData = await type_id.findOne({ '_id': 'ids' });
        const _SYS = await SystemModel.findOne({ '_id': 'SYS' });
       if(!_SYS){
            SystemModel.create(sysJson); 
       }
      if (!_typeData) {
            await type_id.create({});
            await RolesModel.create(rolesJson)
            const role = await RolesModel.findOne({ id: 1 })
            //需要知道角色id名
            if (role) {
                adminJson.power = role._id
                await AdminModel.create(adminJson);
            }
            await PermissionsModel.create(powerJson);
        } else {
            var _spec = await SpecModel.find({})
            if (_spec.length == 0) {
                await SpecModel.create(specJson);
            }
        }
       ctx.body = '导入数据完成!运行后台系统即可';
       // ctx.redirect('http://localhost:9528')
    }
}

export default new dataImport;